﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//new
using System.Resources;
using System.Reflection;
using System.Collections;
using DataRecv.models.road;
using JRCore.tools;
using JRCore.enums;
using JRCore.protocol;

namespace DataRecv
{
    public partial class road : Form
    {
        private Client synclient;
        public road()
        {
            InitializeComponent();
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            try
            {
                addroad form_addroad = new addroad();
                form_addroad.Owner = this;
                form_addroad.ShowDialog();
                form_addroad.Dispose();
            }
            catch { }
        }

        private void button_cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_edit_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.SelectedRows.Count < 1)
                {
                    MessageBox.Show("未选择要修改的记录");
                }
                else
                {
                    editroad form_editroad = new editroad();
                    form_editroad.Owner = this;
                    form_editroad.rowindex = this.dataGridView1.SelectedRows[0].Index;
                    form_editroad.roadid = this.dataGridView1.SelectedRows[0].Cells["roadid"].Value.ToString();
                    form_editroad.roadname = this.dataGridView1.SelectedRows[0].Cells["roadname"].Value.ToString();
                    form_editroad.gbdm = this.dataGridView1.SelectedRows[0].Cells["gbdm"].Value.ToString();
                    form_editroad.kkpath = this.dataGridView1.SelectedRows[0].Cells["kkpath"].Value.ToString();
                    form_editroad.djpath = this.dataGridView1.SelectedRows[0].Cells["djpath"].Value.ToString();
                    form_editroad.kkvpath = this.dataGridView1.SelectedRows[0].Cells["kkvpath"].Value.ToString();
                    form_editroad.djvpath = this.dataGridView1.SelectedRows[0].Cells["djvpath"].Value.ToString();
                    form_editroad.bmbh = this.dataGridView1.SelectedRows[0].Cells["bmbh"].Value.ToString();
                    form_editroad.xzqh = this.dataGridView1.SelectedRows[0].Cells["xzqh"].Value.ToString();
                    form_editroad.bz = this.dataGridView1.SelectedRows[0].Cells["bz"].Value.ToString();
                    form_editroad.ShowDialog();
                    form_editroad.Dispose();
                }
            }
            catch { }
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.SelectedRows.Count < 1)
                {
                    MessageBox.Show("未选择要删除的记录");
                }
                else
                {
                    string roadid = this.dataGridView1.SelectedRows[0].Cells["roadid"].Value.ToString();
                    if (RoadParam.DeleteModel(roadid) == true)
                    {
                        if (G.Roads != null)
                        {
                            G.Roads.Remove(roadid);
                        }
                        this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
                        ((Form_Main)this.Owner).UpDateTree();
                        MessageBox.Show("删除成功");
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
            catch { }
        }

        private void road_Load(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.Rows.Clear();
                if (G.Roads != null)
                {
                    IDictionaryEnumerator ide = G.Roads.GetEnumerator();
                    ide.Reset();
                    while (ide.MoveNext())
                    {
                        RoadParam road = ((Road)ide.Value).param;
                        dataGridView1.Rows.Add(new object[] { road.roadid, road.roadname, road.gbdm, road.kkpath, road.djpath, road.kkvpath, road.djvpath, road.bmbh, road.xzqh, road.bz });
                    }
                }

                if ((G.SystemParam.SynServerIP != "") && (G.SystemParam.SynServerPort != ""))
                {
                    synclient = new Client(G.SystemParam.SynServerIP, int.Parse(G.SystemParam.SynServerPort), Enum_TcpWorkMode.SendRead,0);
                    synclient.OnRecordReceived = new Client.RecordReceived(OnRoadSynFunc);
                }
            }
            catch { }
        }

        private void button_tongbu_Click(object sender, EventArgs e)
        {
            try
            {
                if ((G.Roads != null) && (G.Roads.Count > 0))
                {
                    MessageBox.Show("同步前需清空所有路口");
                    return;
                }

                if (synclient != null)
                {
                    byte[] DataRecvIP = Encoding.UTF8.GetBytes(G.SystemParam.LocalIP);

                    JRCore.protocol.Msg synrequest = new JRCore.protocol.Msg();
                    synrequest.type = Enum_ProtocolType.AddressRequest;
                    synrequest.uuid = Guid.NewGuid().ToString("N");
                    synrequest.feedback = true;
                    synrequest.data = DataRecvIP;
                    synrequest.length = synrequest.data.Length;

                    string error = "";
                    bool flag = synclient.Send(synrequest, ref error);
                    if (flag == true)
                    {
                        this.dataGridView1.Rows.Clear();
                        IDictionaryEnumerator ide = G.Roads.GetEnumerator();
                        ide.Reset();
                        while (ide.MoveNext())
                        {
                            RoadParam p = ((Road)ide.Value).param;
                            if (RoadParam.AddModel(p) == true)
                            {
                                this.dataGridView1.Rows.Add(new object[] { p.roadid, p.roadname, p.gbdm, p.kkpath, p.djpath, p.kkvpath, p.djvpath, p.bmbh, p.xzqh, p.bz });
                            }
                        }
                        ((Form_Main)this.Owner).UpDateTree();
                        MessageBox.Show("同步成功");
                    }
                    else
                    {
                        MessageBox.Show("同步失败");
                    }
                }
                else
                {
                    MessageBox.Show("同步器未启动");
                }
            }
            catch(Exception er)
            {
                MessageBox.Show("同步时出现异常" + er.Message);
            }
        }

        private void road_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (synclient != null)
                {
                    synclient.Dispose();
                }
            }
            catch { }
        }

        private bool OnRoadSynFunc(ref Msg send, Msg read, ref string error)
        {
            bool ans = false;
            try
            {
                if ((send != null) && (read != null))
                {
                    if ((send.type == Enum_ProtocolType.AddressRequest) &&(read.type == Enum_ProtocolType.AddressResponse))
                    {
                        if (send.uuid == read.uuid)
                        {
                            string DataRecvIP = Encoding.UTF8.GetString(send.data);
                            RoadSyn response = RoadSyn.Parse(read.data);
                            if (response != null)
                            {
                                if ((DataRecvIP == response.DataRecvIP) && (DataRecvIP == G.SystemParam.LocalIP))
                                {
                                    if ((response.Count > 0) && (G.Roads != null))
                                    {
                                        G.Roads.Clear();
                                        foreach (RoadNode node in response.RoadNodeList)
                                        {
                                            Road road = new Road();
                                            road.param = new RoadParam();
                                            road.param.roadid = node.roadid;
                                            road.param.gbdm = node.gbdm;
                                            road.param.roadname = node.roadname;
                                            road.param.bmbh = node.bmbh;
                                            road.param.xzqh = node.xzqh;
                                            RoadParam.AddModel(road.param);
                                        }
                                        G.Roads = Road.GetModels();
                                        ans = true;
                                    }
                                }
                                else
                                {
                                    error = "数据终端IP错误";
                                }
                            }
                        }
                        else
                        {
                            error = "uuid不一致";
                        }
                    }
                    else
                    {
                        error = "消息类型错误";
                    }
                }
            }
            catch(Exception er)
            {
                ans = false;
                error = er.Message;
            }
            return ans;
        }

        private void road_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("如参数有修改，重启软件后生效");
            e.Cancel = false;
        }
        
    }
}
